﻿using System;
using System.Data;
using System.Windows.Forms;
using QuanLyTuyenSinh.BussinessLayer;
using QuanLyTuyenSinh.Properties;

namespace QuanLyTuyenSinh.PresentationLayer
{
    public partial class frmEthnic  :DevComponents.DotNetBar.Office2007Form
    {
        private int ma=0;

        public frmEthnic()
        {
            InitializeComponent();
        }
        private void HideControl(bool hien)
        {
            cmdThem.Visible = hien;
            cmdLuu.Visible = hien;
            cmdXoa.Visible = hien;
            cmdThem.Enabled = true;
            cmdXoa.Enabled = true;
        }
        private void FormEthnicLoad(object sender, EventArgs e)
        {
            try
            {
                DataTable table = Operator.Operator.GetListEthnic();
                if (table.Rows.Count > 0)
                {
                    GrEthnic.DataSource = table;
                }
               HideControl(false);
                cmdThem.Visible = true;
               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void CmdThemClick1(object sender, EventArgs e)
        {
            txtEthnicName.Clear();
            HideControl(true);
            ma = 0;
            cmdXoa.Visible = false;
        }

        private void CmdLuuClick1(object sender, EventArgs e)
        {
            try
            {
                if(string.IsNullOrEmpty(txtEthnicName.Text.Trim()))
                {
                    MessageBox.Show("Chưa nhập: Tên dân tộc");
                    return;
                }
                var ethnic = new Ethnic();
                ethnic.EthnicName = txtEthnicName.Text.Trim();
                DataTable listEthnic = Operator.Operator.GetListEthnicById(ma);
                if (listEthnic.Rows.Count > 0)
                {
                    ethnic.EthnicId = ma;
                    int m = ethnic.Update(ethnic);
                    if (m > 0)
                    {
                        FormEthnicLoad(sender, e);
                    }
                }
                else
                {
                    int m = ethnic.Insert(ethnic);
                    if (m > 0)
                    {
                        FormEthnicLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void GrEthnicCellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            if (i < 0) return;
            else
            {
                ma = int.Parse(GrEthnic[0, i].Value.ToString());
                txtEthnicName.Text = GrEthnic[1, i].Value.ToString();
              HideControl(true);
            }
        }

        private void CmdXoaClick(object sender, EventArgs e)
        {
            try
            {
                DialogResult DRL;
                DRL = MessageBox.Show(Resources.Delete, Resources.Header, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (DRL == DialogResult.No)
                {
                    return;
                }
                else
                {
                    var ethnic = new Ethnic();
                    ethnic.EthnicId = ma;
                    int m = ethnic.Delete(ethnic);
                    if (m > 0)
                    {
                        FormEthnicLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void FrmEthnicKeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.S) && cmdLuu.Visible)
            {
                CmdLuuClick1(sender, e);
            }
            else if (e.Control && e.KeyCode == Keys.N)
            {
                CmdThemClick1(sender, e);
            }
            else if ((e.KeyCode == Keys.Delete) && (cmdXoa.Visible))
            {
                CmdXoaClick(sender, e);
            }
        }
    }
}